package lru;

import java.util.HashMap;
import java.util.Map;


//  CKG TODO 2023/5/20:  hashmap 与　linkedlist双向结合　　　　hashmap 用于快速查找　　linkedlist用快速实现增加和删除节点
//  CKG TODO 2023/5/20:使用两种数据结构来达成目的
public class LRUDemo {

    // map 负责查找，构建一个虚拟的双向链表，它里面装的就是一个个 Node 节点，作为数据载体

    // 1.构造一个node节点作为数据载体
    class Node<K, V> {
        K key;
        V value;
        Node<K, V> prev;
        Node<K, V> next;

        public Node() {
            this.prev = this.next = null;
        }

        public Node(K key, V value) {
            this.key = key;
            this.value = value;
            this.prev = this.next = null;
        }
    }


    // 2.构建一个虚拟的双向链表,,里面安放的就是我们的Node
    class DoubleLinkedList<K, V> {
        Node<K, V> head;
        Node<K, V> tail;

        public DoubleLinkedList() {
            head = new Node<>();
            tail = new Node<>();
            head.next = tail;
            tail.prev = head;
        }

        // 3.添加到头
        public void addHead(Node<K, V> node) {
            node.next = head.next;
            node.prev = head;
            head.next.prev = node;
            head.next = node;
        }

        // 4.删除节点
        public void removeNode(Node<K, V> node) {
            node.next.prev = node.prev;
            node.prev.next = node.next;
            node.prev = null;
            node.next = null;
        }

        // 5.获得最后一个节点
        public Node getLast() {
            return tail.prev;
        }
    }


    private int cacheSize;
    Map<Integer, Node<Integer, Integer>> map;
    DoubleLinkedList<Integer, Integer> doubleLinkedList;

    public LRUDemo(int cacheSize) {
        this.cacheSize = cacheSize;//坑位
        map = new HashMap<>();//查找
        doubleLinkedList = new DoubleLinkedList<>();
    }

    public int get(int key) {
        if (!map.containsKey(key)) {
            return -1;
        }
        Node<Integer, Integer> node = map.get(key);
        doubleLinkedList.removeNode(node);
        //  CKG TODO 2023/5/20: 把头作为最近经常使用的元素位置
        doubleLinkedList.addHead(node);
        return node.value;
    }

//  CKG TODO 2023/5/20:   1 添加的ｋｅｙ值存在　就获取ｍａｐ里面的ｋｅｙ对应的ｎｏｄｅ　修改ｖａｌｕｅ　的数值　  删除链表里面原有的ｎｏｄｅ节点　在头部加入修改ｖａｌｕｅ后的ｎｏｄｅ节点
    //  CKG TODO 2023/5/20:  ２，若坑位置满了　　获取链表的最后一个元素　　删除ｍａｐ里面对应的ｋｅｙ　　删除链表中的该node
    //  CKG TODO 2023/5/20: 　　　　若以上俩种情况都不存在　那么就直接在链表的头部插入该ｎｏｄｅ节点　ｍａｐ中也加入对应ｎｏｄｅ的ｋｅｙ
    //  CKG TODO 2023/5/20: 　
    public void put(int key, int value) {
        if (map.containsKey(key)) {  //update
            Node<Integer, Integer> node = map.get(key);
            node.value = value;
            map.put(key, node);
            doubleLinkedList.removeNode(node);
            doubleLinkedList.addHead(node);
        } else {
            if (map.size() == cacheSize)  //坑位满了
            {
                Node<Integer, Integer> lastNode = doubleLinkedList.getLast();
                map.remove(lastNode.key);
                doubleLinkedList.removeNode(lastNode);
            }
            //新增一个
            Node<Integer, Integer> newNode = new Node<>(key, value);
            map.put(key, newNode);
            doubleLinkedList.addHead(newNode);

        }
    }

    public static void main(String[] args) {

        LRUDemo lruCacheDemo = new LRUDemo(3);

        lruCacheDemo.put(1, 1);
        lruCacheDemo.put(2, 2);
        lruCacheDemo.put(3, 3);

//        for (:
//             lruCacheDemo.doubleLinkedList) {
//
//        }

        System.out.println(lruCacheDemo.map.keySet());

        lruCacheDemo.put(4, 1);
        System.out.println(lruCacheDemo.map.keySet());


        lruCacheDemo.put(3, 1);
        System.out.println(lruCacheDemo.map.keySet());
        lruCacheDemo.put(3, 1);
        System.out.println(lruCacheDemo.map.keySet());
        lruCacheDemo.put(3, 1);
        System.out.println(lruCacheDemo.map.keySet());
        lruCacheDemo.put(5, 1);
        System.out.println(lruCacheDemo.map.keySet());

    }
}
